package com.xmhmyh.generalframework.common.utils;


import cn.hutool.core.date.DateUtil;
import com.xmhmyh.generalframework.project.systool.entity.Table;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.springframework.format.datetime.DateFormatter;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 * 代码生成模板处理
 *
 * @author
 */
public class VelocityUtils {

    /**
     * 项目空间路径
     */
    private static final String PROJECT_PATH = "main/java";

    /**
     * mybatis空间路径
     */
    private static final String MYBATIS_PATH = "main/resources/mybatis";

    /**
     * 初始化模版引擎
     *
     * @return 模板列表
     */
    public static VelocityContext getVelocityContext() {
        try {
            Properties p = new Properties();
            // 加载classpath目录下的vm文件
            p.setProperty("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
            // 定义字符集
            p.setProperty(Velocity.ENCODING_DEFAULT, "UTF-8");
            p.setProperty(Velocity.OUTPUT_ENCODING, "UTF-8");
            // 初始化Velocity引擎，指定配置Properties
            Velocity.init(p);
            VelocityContext velocityContext = new VelocityContext();
            return velocityContext;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 获取模板信息
     *
     * @return 模板列表
     */
    public static List<String> getTemplateList(String tplCategory) {
        List<String> templates = new ArrayList<String>();
        templates.add("vm/entity.java.vm");
        templates.add("vm/DTO.java.vm");
        templates.add("vm/VO.java.vm");
        templates.add("vm/mapper.java.vm");
        templates.add("vm/service.java.vm");
        templates.add("vm/serviceImpl.java.vm");
        templates.add("vm/controller.java.vm");
        templates.add("vm/mapper.xml.vm");
//        templates.add("vm/sql/sql.vm");
        templates.add("vm/index_dialog.vue.vm");
        templates.add("vm/api.js.vm");
        return templates;
    }

    /**
     * 设置模板变量信息
     *
     * @return 模板列表
     */
    public static void initParams(VelocityContext velocityContext, Table genTable) {
        String moduleName = genTable.getModuleName();
        String businessName = genTable.getBusinessName();
        String packageName = genTable.getPackageName();
        String tplCategory = genTable.getTplCategory();
        String functionName = genTable.getFunctionName();

        velocityContext.put("tplCategory", genTable.getTplCategory());
        velocityContext.put("tableName", genTable.getTableName());
        velocityContext.put("prefixName", getTablePrefix(genTable.getTableName()));
        velocityContext.put("functionName", StringUtils.isNotEmpty(functionName) ? functionName : "【请填写功能名称】");
        velocityContext.put("ClassName", genTable.getClassName());
        velocityContext.put("className", StringUtils.uncapitalize(genTable.getClassName()));
        velocityContext.put("moduleName", genTable.getModuleName());
        velocityContext.put("BusinessName", StringUtils.capitalize(genTable.getBusinessName()));
        velocityContext.put("businessName", genTable.getBusinessName());
        velocityContext.put("basePackage", getPackagePrefix(packageName));
        velocityContext.put("packageName", packageName);
        velocityContext.put("author", genTable.getFunctionAuthor());
        velocityContext.put("datetime", DateUtil.now());
        velocityContext.put("pkColumn", genTable.getPkColumn());
//        velocityContext.put("importList", getImportList(genTable.getColumns()));
//        velocityContext.put("permissionPrefix", getPermissionPrefix(moduleName, businessName));
        velocityContext.put("columns", genTable.getColumns());
        velocityContext.put("table", genTable);
    }

    public static String getTablePrefix(String tableName) {
        int firstIndex = tableName.indexOf("_");
        String prefix = StringUtils.substring(tableName, 0, firstIndex);
        return prefix;
    }

    /**
     * 获取包前缀
     *
     * @param packageName 包名称
     * @return 包前缀名称
     */
    public static String getPackagePrefix(String packageName) {
        int lastIndex = packageName.lastIndexOf(".");
        String basePackage = StringUtils.substring(packageName, 0, lastIndex);
        return basePackage;
    }

    /**
     * 获取文件名
     */
    public static String getFileName(String template, Table genTable) {
        // 文件名称
        String fileName = "";
        // 包路径
        String packageName = genTable.getPackageName();
        // 模块名
        String moduleName = genTable.getModuleName();
        // 大写类名
        String ClassName = genTable.getClassName();
        // 小写类名
        String className = StringUtils.uncapitalize(genTable.getClassName());
        // 业务名称
        String businessName = genTable.getBusinessName();

        String javaPath = PROJECT_PATH + "/" + StringUtils.replace(packageName, ".", "/");
        String mybatisPath = MYBATIS_PATH + "/" + moduleName;
        String vuePath = "vue";

        if (template.contains("entity.java.vm")) {
            fileName = StringUtils.format("{}/entity/{}.java", javaPath, ClassName);
        } else if (template.contains("DTO.java.vm")) {
            fileName = StringUtils.format("{}/dto/{}.java", javaPath, ClassName + "DTO");
        } else if (template.contains("VO.java.vm")) {
            fileName = StringUtils.format("{}/vo/{}.java", javaPath, ClassName + "VO");
        } else if (template.contains("mapper.java.vm")) {
            fileName = StringUtils.format("{}/mapper/{}Mapper.java", javaPath, ClassName);
        } else if (template.contains("service.java.vm")) {
            fileName = StringUtils.format("{}/service/I{}Service.java", javaPath, ClassName);
        } else if (template.contains("serviceImpl.java.vm")) {
            fileName = StringUtils.format("{}/service/impl/{}ServiceImpl.java", javaPath, ClassName);
        } else if (template.contains("controller.java.vm")) {
            fileName = StringUtils.format("{}/controller/{}Controller.java", javaPath, ClassName);
        } else if (template.contains("mapper.xml.vm")) {
            fileName = StringUtils.format("{}/{}Mapper.xml", mybatisPath, ClassName);
        }else if (template.contains("api.js.vm")) {
            fileName = StringUtils.format("{}/api/{}.js", vuePath, className);
        } else if (template.contains("index_dialog.vue.vm")) {
            fileName = StringUtils.format("{}/views/{}/index.vue", vuePath, className);
        }
        return fileName;
    }

    /**
     * @Author: 于丁
     * @Description: 添加参数到模版
     * @DateTime: 2020/7/22 5:19 下午
     * @Params:
     * @Return
     */
    public static void putParams(VelocityContext context, String key, Object value) {
        context.put(key, value);
    }
}
